Page({
  data: {
    Arr: [],
    playing: false,
    audioContext: null,
    moreshow: false  // 新增加载状态显示控制
  },


  onLoad(options) {
    this.loadAudioList();
  },


  loadAudioList: function () {
    wx.showLoading({
      title: '加载中...'
    });
    wx.request({
      url: 'http://127.0.0.1:9090/Audio/selectAll',
      method: 'GET',
      success: (res) => {
        if (res.data && res.data.data) {  // 增加数据检查
          this.setData({
            Arr: res.data.data
          });
          console.log(this.data.Arr);
          this.loadImages();
        } else {
          wx.showToast({
            title: '数据格式错误',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '加载音频列表失败',
          icon: 'none'
        });
        console.error('Failed to fetch audio list from server:', err);
      },
      complete: () => {
        wx.hideLoading();
      }
    });
  },


  loadImages: function () {
    const promises = this.data.Arr.map(item => this.getImage(item));
    Promise.all(promises)
   .then(() => {
        console.log('All images loaded');
      })
   .catch((error) => {
        console.error('Image loading error:', error);
        wx.showToast({
          title: '部分图片加载失败，请稍后重试',
          icon: 'none'
        });
      })
   .finally(() => {
        wx.hideLoading();
      });
  },


  getImage: function (item) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'http://127.0.0.1:9090/file/download2',
        method: 'GET',
        data: {
          fileName: item.image
        },
        success: (res) => {
          if (res.data) {
            item.image = 'data:image/png;base64,' + res.data;
            let index = this.data.Arr.findIndex(c => c === item);
            let newArr = [...this.data.Arr];
            newArr[index] = item;
            this.setData({
              Arr: newArr
            });
            resolve();
          } else {
            reject(new Error('No image data received'));
          }
        },
        fail: (err) => {
          wx.showToast({
            title: '图片加载失败',
            icon: 'none'
          });
          console.error('Failed to load image:', err);
          reject(err);
        }
      });
    });
  },


  playAudio: function (e) {
    const src = e.currentTarget.dataset.src;  // 正确获取参数
    if (!this.data.audioContext) {
      this.setData({
        audioContext: wx.createInnerAudioContext()
      });
    }
    const audioContext = this.data.audioContext;
    audioContext.src = src;
    audioContext.onPlay(() => {
      this.setData({
        playing: true
      });
    });
    audioContext.onPause(() => {
      this.setData({
        playing: false
      });
    });
    audioContext.onEnded(() => {
      this.setData({
        playing: false
      });
    });
    audioContext.play();
  },


  pauseAudio: function () {
    const audioContext = this.data.audioContext;
    if (audioContext) {
      audioContext.pause();
    }
  },


  stopAudio: function () {
    const audioContext = this.data.audioContext;
    if (audioContext) {
      audioContext.stop();
      this.setData({
        audioContext: null,
        playing: false
      });
    }
  },


  onReady() {


  },


  onShow() {
    // 页面显示时的操作
  },


  onUnload() {
    const audioContext = this.data.audioContext;
    if (audioContext) {
      audioContext.destroy();
    }
  },


  onPullDownRefresh() {
    this.onLoad();
    wx.stopPullDownRefresh();
  },


  onReachBottom() {
    // 上拉触底的操作
  },


  onShareAppMessage() {
    return {
      title: '分享标题',
      path: '/page/path'
    };
  }
});